import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/token'
// import Login from '@/views/Login'
// import Home from '@/views/Home'
// import Layout from '@/views/Layout'
// import User from '@/views/User'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEditor from '@/views/User/UserEdit'
// import Chat from '@/views/Chat'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"Login" */'@/views/Login'),
    // 路由独享守卫
    beforeEnter (to, from, next) {
      // 需求: 如果已经登陆了,就不能切换到登录页
      if (getToken()?.length > 0) {
        next('/layout/home') // 留在原地/ 去首页
      } else {
        next() // 其他情况通行
      }
    }
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName:"Layout" */'@/views/Layout'),
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName:"Home" */'@/views/Home'),
        meta: {
          scrollT: 0 // 保存离开时,滚动条位置
        }
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName:"User" */'@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName:"Search" */'@/views/Search')
  },
  {
    path: '/search_result:kw',
    component: () => import(/* webpackChunkName:"SearchResult" */'@/views/Search/SearchResult.vue')
  },
  {
    path: '/detail',
    component: () => import(/* webpackChunkName:"ArticleDetail" */'@/views/ArticleDetail')
  },
  {
    path: '/user_edit',
    component: () => import(/* webpackChunkName:"UserEdit" */'@/views/User/UserEdit')
  },
  {
    path: '/chat',
    component: () => import(/* webpackChunkName:"Chat" */'@/views/Chat')
  }
]

const router = new VueRouter({
  routes
})
// 路由前置守卫
// router.beforeEach((to, from, next) => {
//   // 需求: 如果已经登陆了,就不能切换到登录页
//   if (getToken()?.length > 0 && to.path === '/login') {
//     next(false) // 留在原地
//   } else {
//     next() // 其他情况通行
//   }
// })
export default router
